package com.qing.controller;

import com.qing.config.security.pojo.JwtUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

import com.qing.service.DocumentService;
import com.qing.pojo.Document;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author qinghua
 * @since 2022-06-06
 */
@RestController
@RequestMapping("/document")
public class DocumentController {
    @Autowired
    private DocumentService documentService;

    @PostMapping("/insert")
    public Boolean save(@RequestBody Document document) {
        return documentService.saveOrUpdate(document);
    }

    @GetMapping("/deleteById/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return documentService.removeById(id);
    }

    @GetMapping("/queryAll")
    public List<Document> queryAll() {
        return documentService.list();
    }

    @GetMapping("/queryById/{id}")
    public Document findOne(@PathVariable("id") Integer id) {
        return documentService.getById(id);
    }

    @GetMapping("/queryAllAndName")
    public List<Map<String, Object>> queryAllAndName() {
        return documentService.queryAllAndName();
    }

    @PostMapping("/upload")
    public Boolean upload(@RequestParam("file") MultipartFile file,
                          @RequestParam("title") String title,
                          @RequestParam("remake") String remake,
                          @AuthenticationPrincipal JwtUser jwtUser){
        return documentService.upload(file,title,remake,jwtUser.getId());
    }

}

